package year2023.sort;

public class Code00_BSExist {

    //二分查找法
    public static boolean exist(int[] sortedArr, int num) {
        if(sortedArr == null || sortedArr.length == 0) {
            return false;
        }

        int L = 0;
        int R = sortedArr.length - 1;
        int mid = 0;
        while(L <= R) {
            //N / 2 == N >> 1
            //N * 2 == N << 1
            //N * 2 + 1 == (N<<1) | 1
            mid = L + ((R - L) >> 1);
            // mid = (L + R) / 2 有可能溢出
            if(sortedArr[mid] == num) {
                return true;
            }else if(sortedArr[mid] > num) {
                R = mid - 1;
            }else {
                L = mid + 1;
            }
        }
        return sortedArr[L] == num;
    }

    public static void main(String[] args) {
        int[] arr = {1,2,3,2,3};
        System.out.println(exist(arr,0));
    }
}
